﻿var pager;
var grid;
var menu;
var query = {};
query.pageSize = 20;
query.pageNo = 1;

jQuery(function ()
{
    //搜索
    $("body").click(function ()
    {
        $("#menu").hide();
        $("#advance").hide();
    });
    $("#advance").click(function (event)
    {
        event.stopPropagation();
    });
    $(".k-popup").click(function (event)
    {
        $("body").unbind("click");
    });
    $(".k-popup").mouseout(function (event)
    {
        $("body").click(function ()
        {
            $("#advance").hide();
        });
    });
    $("#keyword").next("button").click(function (event)
    {
        event.stopPropagation();
        $("#advance").toggle();
    });
    $("#keyword").find(".k-i-search").click(function ()
    {
        searchKeyword();
    });
    $("#keyword").find(":text").keydown(function (e)
    {
        if (e.keyCode == 13)
        {
            searchKeyword();
            return false;
        }
    });
    $("#advance").find(":button").click(function ()
    {
        searchAdvance();
    });
    $("#advance").keydown(function (e)
    {
        if (e.keyCode == 13)
        {
            searchAdvance();
        }
    });
    $("#advance").find(":reset").click(function ()
    {
        searchReset();
    });
    $("#toolbar").find(":text").keydown(function (e)
    {
        if (e.keyCode == 13)
        {
            search();
            return false;
        }
    });
    $("#filterbar").find(":button").click(function ()
    {
        filter();
    });
    $("#filterbar").keydown(function (e)
    {
        if (e.keyCode == 13)
        {
            filter();
        }
    });
    $("#filterbar").find(":reset").click(function ()
    {
        filterReset();
    });

    //右键菜单
    menu = $("#menu").kendoMenu().data("kendoMenu");

    //分页
    pager = jQuery("#pager").kendoPager(
        {
            messages:
            {
                display: "第{0}-{1}项 总计{2}项",
                empty: "总计0项",
                page: "页",
                of: "第{0}",
                itemsPerPage: "项每页",
                refresh: "刷新",
                first: "首页",
                previous: "上页",
                next: "下页",
                last: "未页"
            },

            change: function (e)
            {
                query.pageNo = e.index;
                reload();
            }
        }).data("kendoPager");
});

function bind(result)
{
    var total = resultHelper.total(result);
    var html = resultHelper.html(result);

    //构造分页
    var dataSource = new kendo.data.DataSource(
        {
            data: new Array(total),
            pageSize: query.pageSize,
            page: query.pageNo
        });
    pager.setDataSource(dataSource);
    pager.refresh();

    //构造表格
    $("#grid").html(html);
    var ids = [];
    var widths = [];
    $("#grid").find("th").each(function ()
    {
        var field = $(this).attr("data-field");
        if (!field)
        {
            $(this).attr("data-field", $(this).text().trim());
        }
        else
        {
            field = field.replace(".", "_");
            $(this).attr("data-field", field)
            $(this).addClass("orderby");
        }
        widths.push($(this).attr("width"));

        if (query.orderBy)
        {
            if (query.orderBy.replace(".", "_") == field)
            {
                $(this).html($(this).text() + '<span class="k-icon k-i-arrow-n"></span>');
            }
            else if (query.orderBy.replace(".", "_") == field + " DESC")
            {
                $(this).html($(this).text() + '<span class="k-icon k-i-arrow-s"></span>');
            }
        }
    });
    $("#grid").find("tbody tr").each(function ()
    {
        ids.push($(this).attr("id"));
    });
    $("#grid").find("tbody tr").each(function ()
    {
        ids.push($(this).attr("id"));
    });
    var height = $("body").height() - $("#toolbar").height() - 35 - 50;
    grid = $("#grid").find("table").kendoGrid(
        {
            selectable: "multiple",
            reorderable: true,
            resizable: true,
            height: height
        }).data("kendoGrid");
    $.each(widths, function (index, item)
    {
        if (item) $("#grid").find("tbody tr td:eq(" + index + ")").width(item);
    });
    $("#grid").find("tbody tr").each(function (i)
    {
        $(this).attr("id", ids[i])
    });
    $("#grid").find("tbody tr").dblclick(function ()
    {
        if (window.dbclick)
        {
            window.dbclick();
        }
        else if (window.detail)
        {
            window.detail();
        }
        else if (window.items)
        {
            window.items();
        }
        else if (window.modify)
        {
            window.modify();
        }
    });
    $("#grid").find("tbody tr").bind('contextmenu', function (e)
    {
        //当菜单超出窗口边界时处理
        var left = e.pageX;
        var top = e.pageY;
        var p =
            {
                wh: $("body").height(),
                ww: $("body").width(),
                mh: $("#menu").height(),
                mw: $("#menu").width()
            };
        left = (left + p.mw) >= p.ww ? (left -= p.mw) : left;
        top = (top + p.mh) >= p.wh ? (top -= p.mh) : top;
        $("#menu").css(
            {
                zIndex: 10000,
                left: left,
                top: top
            });
        $("#menu").show();
        menu.open(".k-first");

        //选中
        grid.clearSelection();
        grid.select(this);

        return false;
    });

    $("#grid").find(".orderby").click(function ()
    {
        var field = $(this).attr("data-field").replace("_", ".");
        if (query.orderBy == field)
        {
            query.orderBy = field + " DESC";
        }
        else
        {
            query.orderBy = field;
        }
        reload();
    });
}

function exportData(url, fileType)
{
    window.open(url + "Export?FileType=" + fileType);
}

function search()
{
    var input = $("#toolbar").find(":text");
    var params = input.attr("name") + "=" + input.val();
    var pageSize = query.pageSize;
    var orderBy = query.orderBy;

    query = paramsToObject(params);
    query.pageNo = 1;
    query.pageSize = pageSize;
    query.orderBy = orderBy;
    reload();
}

function searchKeyword()
{
    var form = $("#advance").find("form");
    var input = $("#keyword").find(":text");
    var params = input.attr("name") + "=" + input.val();
    var pageSize = query.pageSize;
    var orderBy = query.orderBy;

    form[0].reset();
    query = paramsToObject(params);
    query.pageNo = 1;
    query.pageSize = pageSize;
    query.orderBy = orderBy;
    reload();
}

function searchAdvance()
{
    var form = $("#advance").find("form");
    var input = $("#keyword").find(":text");
    var pageSize = query.pageSize;
    var orderBy = query.orderBy;

    input.val("");
    query = form.serializeObject();
    query.pageNo = 1;
    query.pageSize = pageSize;
    query.orderBy = orderBy;
    reload();
}

function searchReset()
{
    var form = $("#advance").find("form");
    var input = $("#keyword").find(":text");
    var pageSize = query.pageSize;
    var orderBy = query.orderBy;

    input.val("");
    form[0].reset();
    query = form.serializeObject();
    query.pageNo = 1;
    query.pageSize = pageSize;
    query.orderBy = orderBy;
}

function filter()
{
    var form = $("#filterbar").find("form");
    var pageSize = query.pageSize;
    var orderBy = query.orderBy;

    query = form.serializeObject();
    query.pageNo = 1;
    query.pageSize = pageSize;
    query.orderBy = orderBy;
    reload();
}

function filterReset()
{
    var form = $("#filterbar").find("form");
    var pageSize = query.pageSize;
    var orderBy = query.orderBy;

    form[0].reset();
    query = form.serializeObject();
    query.pageNo = 1;
    query.pageSize = pageSize;
    query.orderBy = orderBy;
}

function gridSelected()
{
    var ids = [];
    $("#grid").find(".k-state-selected").each(function ()
    {
        ids.push($(this).attr("id"));
    });
    if (ids.length == 0)
    {
        return null;
    }
    return ids;
}

function gridSelectedHidden(name)
{
    var datas = [];
    $("#grid").find(".k-state-selected :hidden[name='" + name + "']").each(function ()
    {
        datas.push($(this).val());
    });
    if (datas.length == 0)
    {
        return null;
    }
    return datas;
}

function nav(url)
{
    top.navTab(url);
}

function back(reload, data, message, delay)
{
    top.backTab(reload, data, message, delay)
}

function reload(data)
{
    read();
}